// 班次管理
#include "tab1.h"
#include <stdio.h>
#include <string.h>

// 静态变量声明
static lv_obj_t *TrainNumber_ta = NULL;
static lv_obj_t *start_ta = NULL;
static lv_obj_t *end_ta = NULL;
static lv_obj_t *departtime_ta = NULL;
static lv_obj_t *date_ta = NULL;
static lv_obj_t *ArriveTime_ta = NULL;
static lv_obj_t *price_ta = NULL;
static lv_obj_t *totalseats_ta = NULL;
static lv_obj_t *kb = NULL;
static lv_obj_t *result_label = NULL;

// 输入框获取焦点时的回调函数
static void focus_cb(lv_event_t * e);
// 输入框失去焦点时的回调函数
static void defocus_cb(lv_event_t * e);

// 清空所有输入框
static void clear_all_fields() 
{
    lv_textarea_set_text(TrainNumber_ta, "");
    lv_textarea_set_text(start_ta, "");
    lv_textarea_set_text(end_ta, "");
    lv_textarea_set_text(departtime_ta, "");
    lv_textarea_set_text(date_ta, "");
    lv_textarea_set_text(ArriveTime_ta, "");
    lv_textarea_set_text(price_ta, "");
    lv_textarea_set_text(totalseats_ta, "");
}

// 显示结果消息
static void show_result(const char *message, lv_color_t color) 
{
    lv_label_set_text(result_label, message);
    lv_obj_set_style_text_color(result_label, color, 0);
    lv_obj_clear_flag(result_label, LV_OBJ_FLAG_HIDDEN);
}

// 添加班次按钮事件处理
static void add_btn_event_cb(lv_event_t *e) 
{
    // 获取输入框内容
    const char *train_number = lv_textarea_get_text(TrainNumber_ta);
    const char *start = lv_textarea_get_text(start_ta);
    const char *end = lv_textarea_get_text(end_ta);
    const char *depart_time = lv_textarea_get_text(departtime_ta);
    const char *date = lv_textarea_get_text(date_ta);
    const char *arrive_time = lv_textarea_get_text(ArriveTime_ta);
    const char *price_str = lv_textarea_get_text(price_ta);
    const char *total_seats_str = lv_textarea_get_text(totalseats_ta);

    // 验证输入
    if (strlen(train_number) == 0 || strlen(start) == 0 || strlen(end) == 0 ||
        strlen(depart_time) == 0 || strlen(date) == 0 || strlen(arrive_time) == 0 ||
        strlen(price_str) == 0 || strlen(total_seats_str) == 0) 
        {
        show_result("请填写所有字段!", lv_color_hex(0xFF0000));
        return;
    }

    // 转换为数值
    double price = atof(price_str);
    int total_seats = atoi(total_seats_str);

    if (price <= 0 || total_seats <= 0) {
        show_result("价格和座位数必须大于0!", lv_color_hex(0xFF0000));
        return;
    }

    // 检查班次是否已存在
    Train *existing_train = findTrain(trainList, train_number);
    if (existing_train != NULL) {
        show_result("班次已存在!", lv_color_hex(0xFF0000));
        return;
    }

    // 创建新班次
    Train *new_train = (Train*)malloc(sizeof(Train));
    if (!new_train) {
        show_result("内存分配失败!", lv_color_hex(0xFF0000));
        return;
    }

    // 设置班次信息
    strncpy(new_train->TrainNumber, train_number, sizeof(new_train->TrainNumber));
    strncpy(new_train->start, start, sizeof(new_train->start));
    strncpy(new_train->end, end, sizeof(new_train->end));
    strncpy(new_train->DepartTime, depart_time, sizeof(new_train->DepartTime));
    strncpy(new_train->date, date, sizeof(new_train->date));
    strncpy(new_train->ArriveTime, arrive_time, sizeof(new_train->ArriveTime));

    // 创建座位信息
    new_train->seatCount = 1;
    new_train->seats = (SeatInfo*)malloc(sizeof(SeatInfo));
    if (!new_train->seats) {
        free(new_train);
        show_result("内存分配失败!", lv_color_hex(0xFF0000));
        return;
    }

    // 设置座位信息
    new_train->seats[0].SeatPrice = price;
    new_train->seats[0].TotalSeats = total_seats;
    new_train->seats[0].AvailableSeats = total_seats;

    // 创建座位状态字符串 (全部可用)
    new_train->seats[0].SeatStatus = (char*)malloc(total_seats + 1);
    if (!new_train->seats[0].SeatStatus) 
    {
        free(new_train->seats);
        free(new_train);
        show_result("内存分配失败!", lv_color_hex(0xFF0000));
        return;
    }

    memset(new_train->seats[0].SeatStatus, '1', total_seats);
    new_train->seats[0].SeatStatus[total_seats] = '\0';

    // 创建座位号数组
    new_train->seats[0].SeatNumbers = (char**)malloc(total_seats * sizeof(char*));
    if (!new_train->seats[0].SeatNumbers) 
    {
        free(new_train->seats[0].SeatStatus);
        free(new_train->seats);
        free(new_train);
        show_result("内存分配失败!", lv_color_hex(0xFF0000));
        return;
    }

    for (int i = 0; i < total_seats; i++) 
    {
        new_train->seats[0].SeatNumbers[i] = (char*)malloc(10);
        if (!new_train->seats[0].SeatNumbers[i])
         {
            for (int j = 0; j < i; j++)
            {
                free(new_train->seats[0].SeatNumbers[j]);
            }
            free(new_train->seats[0].SeatNumbers);
            free(new_train->seats[0].SeatStatus);
            free(new_train->seats);
            free(new_train);
            show_result("内存分配失败!", lv_color_hex(0xFF0000));
            return;
        }
        snprintf(new_train->seats[0].SeatNumbers[i], 10, "%d", i + 1);
    }

    new_train->next = NULL;

    // 添加到链表
    addTrain(trainList, new_train);

    // 保存到文件
    if (saveTrainsToFile(trainList, "bendi.json")) 
    {
        show_result("班次添加成功!", lv_color_hex(0x00FF00));
        clear_all_fields();
    } else {
        show_result("保存文件失败!", lv_color_hex(0xFF0000));
    }
}

// 删除班次按钮事件处理
static void delete_btn_event_cb(lv_event_t *e) {
    const char *train_number = lv_textarea_get_text(TrainNumber_ta);

    if (strlen(train_number) == 0) {
        show_result("请输入班次号!", lv_color_hex(0xFF0000));
        return;
    }

    // 查找班次
    Train *prev = NULL;
    Train *current = trainList->head;

    while (current != NULL) {
        if (strcmp(current->TrainNumber, train_number) == 0) {
            // 找到班次，从链表中移除
            if (prev == NULL) {
                trainList->head = current->next;
            } else {
                prev->next = current->next;
            }

            // 释放内存
            freeSeatInfo(current->seats, current->seatCount);
            free(current);
            trainList->count--;

            // 保存到文件
            if (saveTrainsToFile(trainList, "bendi.json")) {
                show_result("班次删除成功!", lv_color_hex(0x00FF00));
                clear_all_fields();
            } else {
                show_result("保存文件失败!", lv_color_hex(0xFF0000));
            }

            return;
        }

        prev = current;
        current = current->next;
    }

    show_result("未找到该班次!", lv_color_hex(0xFF0000));
}

// 查找班次按钮事件处理
static void search_btn_event_cb(lv_event_t *e) {
    const char *train_number = lv_textarea_get_text(TrainNumber_ta);

    if (strlen(train_number) == 0) {
        show_result("请输入班次号!", lv_color_hex(0xFF0000));
        return;
    }

    // 查找班次
    Train *train = findTrain(trainList, train_number);

    if (train == NULL) {
        show_result("未找到该班次!", lv_color_hex(0xFF0000));
        return;
    }

    // 填充表单
    lv_textarea_set_text(start_ta, train->start);
    lv_textarea_set_text(end_ta, train->end);
    lv_textarea_set_text(departtime_ta, train->DepartTime);
    lv_textarea_set_text(date_ta, train->date);
    lv_textarea_set_text(ArriveTime_ta, train->ArriveTime);

    if (train->seatCount > 0) {
        char price_buf[20];
        snprintf(price_buf, sizeof(price_buf), "%.2f", train->seats[0].SeatPrice);
        lv_textarea_set_text(price_ta, price_buf);

        char seats_buf[20];
        snprintf(seats_buf, sizeof(seats_buf), "%d", train->seats[0].TotalSeats);
        lv_textarea_set_text(totalseats_ta, seats_buf);
    }

    show_result("班次查找成功!", lv_color_hex(0x00FF00));
}

// 修改班次按钮事件处理
static void modify_btn_event_cb(lv_event_t *e)
 {
    const char *train_number = lv_textarea_get_text(TrainNumber_ta);

    if (strlen(train_number) == 0) 
    {
        show_result("请输入班次号!", lv_color_hex(0xFF0000));
        return;
    }

    // 查找班次
    Train *train = findTrain(trainList, train_number);
    if (train == NULL) 
    {
        show_result("未找到该班次!", lv_color_hex(0xFF0000));
        return;
    }

    // 读取新数据
    const char *start = lv_textarea_get_text(start_ta);
    const char *end = lv_textarea_get_text(end_ta);
    const char *depart_time = lv_textarea_get_text(departtime_ta);
    const char *date = lv_textarea_get_text(date_ta);
    const char *arrive_time = lv_textarea_get_text(ArriveTime_ta);
    const char *price_str = lv_textarea_get_text(price_ta);
    const char *total_seats_str = lv_textarea_get_text(totalseats_ta);

    if (strlen(start) == 0 || strlen(end) == 0 || strlen(depart_time) == 0 ||
        strlen(date) == 0 || strlen(arrive_time) == 0 || strlen(price_str) == 0 ||
        strlen(total_seats_str) == 0) 
        {
        show_result("请填写所有字段!", lv_color_hex(0xFF0000));
        return;
    }

    double price = atof(price_str);
    int total_seats = atoi(total_seats_str);
    if (price <= 0 || total_seats <= 0) 
    {
        show_result("价格和座位数必须大于0!", lv_color_hex(0xFF0000));
        return;
    }

    // 更新信息
    strncpy(train->start, start, sizeof(train->start));
    strncpy(train->end, end, sizeof(train->end));
    strncpy(train->DepartTime, depart_time, sizeof(train->DepartTime));
    strncpy(train->date, date, sizeof(train->date));
    strncpy(train->ArriveTime, arrive_time, sizeof(train->ArriveTime));

    if (train->seatCount > 0) 
    {
        SeatInfo *seat = &train->seats[0];
        int old_total = seat->TotalSeats;
        double old_price = seat->SeatPrice;

        // 检查价格或座位数是否变化
        if (old_price != price || old_total != total_seats) 
        {
            // 释放旧的内存
            free(seat->SeatStatus);
            for (int i = 0; i < old_total; i++) 
            {
                free(seat->SeatNumbers[i]);
            }
            free(seat->SeatNumbers);

            // 分配新的内存
            seat->SeatStatus = (char*)malloc(total_seats + 1);
            if (!seat->SeatStatus) 
            {
                show_result("内存分配失败!", lv_color_hex(0xFF0000));
                return;
            }
            memset(seat->SeatStatus, '1', total_seats);
            seat->SeatStatus[total_seats] = '\0';

            seat->SeatNumbers = (char**)malloc(total_seats * sizeof(char*));
            if (!seat->SeatNumbers) 
            {
                free(seat->SeatStatus);
                show_result("内存分配失败!", lv_color_hex(0xFF0000));
                return;
            }
            for (int i = 0; i < total_seats; i++) 
            {
                seat->SeatNumbers[i] = (char*)malloc(10);
                if (!seat->SeatNumbers[i]) {
                    for (int j = 0; j < i; j++) {
                        free(seat->SeatNumbers[j]);
                    }
                    free(seat->SeatNumbers);
                    free(seat->SeatStatus);
                    show_result("内存分配失败!", lv_color_hex(0xFF0000));
                    return;
                }
                snprintf(seat->SeatNumbers[i], 10, "%d", i + 1);
            }

            seat->SeatPrice = price;
            seat->TotalSeats = total_seats;
            seat->AvailableSeats = total_seats; // 重置为全部可用
        } else {
            // 如果座位数和价格没有变化，只更新价格
            seat->SeatPrice = price;
        }
    }

    // 保存
    if (saveTrainsToFile(trainList, "bendi.json")) 
    {
        show_result("班次修改成功!", lv_color_hex(0x00FF00));
    } else 
    {
        show_result("保存文件失败!", lv_color_hex(0xFF0000));
    }
}

void init_user_management_tab1(lv_obj_t *parent) 
{   
    // 创建容器用于更好的布局
    lv_obj_t *container = lv_obj_create(parent);
    lv_obj_set_size(container, 950, 500);
    lv_obj_center(container);
    lv_obj_set_flex_flow(container, LV_FLEX_FLOW_ROW_WRAP);
    lv_obj_set_flex_align(container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_clear_flag(container, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(container, 0, 0);
    
    // 左侧输入区域
    lv_obj_t *left_col = lv_obj_create(container);
    lv_obj_set_size(left_col, lv_pct(45), lv_pct(86));
    lv_obj_set_flex_flow(left_col, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(left_col, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_clear_flag(left_col, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(left_col, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(left_col, 0, 0);
    
    // 右侧输入区域
    lv_obj_t *right_col = lv_obj_create(container);
    lv_obj_set_size(right_col, lv_pct(45), lv_pct(86));
    lv_obj_set_flex_flow(right_col, LV_FLEX_FLOW_COLUMN);
    lv_obj_set_flex_align(right_col, LV_FLEX_ALIGN_START, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_clear_flag(right_col, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(right_col, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(right_col, 0, 0);
    
    // 班次号输入框格式(Dxxxxx)
    lv_obj_t *TrainNumber_label = lv_label_create(left_col);
    lv_label_set_text(TrainNumber_label, "班次号:");
    lv_obj_set_style_text_font(TrainNumber_label, font_chinese, 0);
    lv_obj_set_style_text_color(TrainNumber_label, lv_color_white(), 0);
    
    TrainNumber_ta = lv_textarea_create(left_col);
    lv_textarea_set_placeholder_text(TrainNumber_ta, "G1234");
    lv_obj_set_style_text_font(TrainNumber_ta, font_chinese, 0);
    lv_obj_set_size(TrainNumber_ta, lv_pct(90), 30);              // 缩小
    lv_textarea_set_max_length(TrainNumber_ta, 20);
    lv_textarea_set_one_line(TrainNumber_ta, true);
    lv_obj_align(TrainNumber_ta, LV_ALIGN_CENTER, 0, 0);

    // 出发点start输入框
    lv_obj_t *start_label = lv_label_create(left_col);
    lv_label_set_text(start_label, "起点:");
    lv_obj_set_style_text_font(start_label, font_chinese, 0);
    lv_obj_set_style_text_color(start_label, lv_color_white(), 0);
    
    start_ta = lv_textarea_create(left_col);
    lv_textarea_set_placeholder_text(start_ta, "北京");
    lv_obj_set_style_text_font(start_ta, font_chinese, 0);
    lv_obj_set_size(start_ta, lv_pct(90), 30);
    lv_textarea_set_max_length(start_ta, 20);
    lv_textarea_set_one_line(start_ta, true);
    lv_obj_align(start_ta, LV_ALIGN_CENTER, 0, 0);

    // 终点end输入框
    lv_obj_t *end_label = lv_label_create(left_col);
    lv_label_set_text(end_label, "终点:");
    lv_obj_set_style_text_font(end_label, font_chinese, 0);
    lv_obj_set_style_text_color(end_label, lv_color_white(), 0);
    
    end_ta = lv_textarea_create(left_col);
    lv_textarea_set_placeholder_text(end_ta, "上海");
    lv_obj_set_style_text_font(end_ta, font_chinese, 0);
    lv_obj_set_size(end_ta, lv_pct(90), 30);
    lv_textarea_set_max_length(end_ta, 20);
    lv_textarea_set_one_line(end_ta, true);
    lv_obj_align(end_ta, LV_ALIGN_CENTER, 0, 0);

    // 出发时间depattime输入框格式（xx:xx）
    lv_obj_t *departtime_label = lv_label_create(left_col);
    lv_label_set_text(departtime_label, "出发时间:");
    lv_obj_set_style_text_font(departtime_label, font_chinese, 0);
    lv_obj_set_style_text_color(departtime_label, lv_color_white(), 0);
    
    departtime_ta = lv_textarea_create(left_col);
    lv_textarea_set_placeholder_text(departtime_ta, "08:30");
    lv_obj_set_style_text_font(departtime_ta, font_chinese, 0);
    lv_obj_set_size(departtime_ta, lv_pct(90), 30);
    lv_textarea_set_max_length(departtime_ta, 20);
    lv_textarea_set_one_line(departtime_ta, true);
    lv_obj_align(departtime_ta, LV_ALIGN_CENTER, 0, 0);

    // 日期(格式2025-xx-xx)
    lv_obj_t *date_label = lv_label_create(right_col);
    lv_label_set_text(date_label, "日期:");
    lv_obj_set_style_text_font(date_label, font_chinese, 0);
    lv_obj_set_style_text_color(date_label, lv_color_white(), 0);
    
    date_ta = lv_textarea_create(right_col);
    lv_textarea_set_placeholder_text(date_ta, "2025-01-01");
    lv_obj_set_style_text_font(date_ta, font_chinese, 0);
    lv_obj_set_size(date_ta, lv_pct(90), 30);
    lv_textarea_set_max_length(date_ta, 20);
    lv_textarea_set_one_line(date_ta, true);
    lv_obj_align(date_ta, LV_ALIGN_CENTER, 0, 0);

    // 到达时间输入框(xx:xx)
    lv_obj_t *ArriveTime_label = lv_label_create(right_col);
    lv_label_set_text(ArriveTime_label, "到达时间:");
    lv_obj_set_style_text_font(ArriveTime_label, font_chinese, 0);
    lv_obj_set_style_text_color(ArriveTime_label, lv_color_white(), 0);
    
    ArriveTime_ta = lv_textarea_create(right_col);
    lv_textarea_set_placeholder_text(ArriveTime_ta, "16:45");
    lv_obj_set_style_text_font(ArriveTime_ta, font_chinese, 0);
    lv_obj_set_size(ArriveTime_ta, lv_pct(90), 30);
    lv_textarea_set_max_length(ArriveTime_ta, 20);
    lv_textarea_set_one_line(ArriveTime_ta, true);
    lv_obj_align(ArriveTime_ta, LV_ALIGN_CENTER, 0, 0);

    // 价格输入框(数字)
    lv_obj_t *price_label = lv_label_create(right_col);
    lv_label_set_text(price_label, "价格:");
    lv_obj_set_style_text_font(price_label, font_chinese, 0);
    lv_obj_set_style_text_color(price_label, lv_color_white(), 0);
    
    price_ta = lv_textarea_create(right_col);
    lv_textarea_set_placeholder_text(price_ta, "268.5");
    lv_obj_set_style_text_font(price_ta, font_chinese, 0);
    lv_obj_set_size(price_ta, lv_pct(90), 30);
    lv_textarea_set_max_length(price_ta, 20);
    lv_textarea_set_one_line(price_ta, true);
    lv_obj_align(price_ta, LV_ALIGN_CENTER, 0, 0);

    // 座位总数
    lv_obj_t *totalseats_label = lv_label_create(right_col);
    lv_label_set_text(totalseats_label, "座位数:");
    lv_obj_set_style_text_font(totalseats_label, font_chinese, 0);
    lv_obj_set_style_text_color(totalseats_label, lv_color_white(), 0);
    
    totalseats_ta = lv_textarea_create(right_col);
    lv_textarea_set_placeholder_text(totalseats_ta, "120");
    lv_obj_set_style_text_font(totalseats_ta, font_chinese, 0);
    lv_obj_set_size(totalseats_ta, lv_pct(90), 30);
    lv_textarea_set_max_length(totalseats_ta, 20);
    lv_textarea_set_one_line(totalseats_ta, true);
    lv_obj_align(totalseats_ta, LV_ALIGN_CENTER, 0, 0);

    // 按钮容器
    lv_obj_t *btn_container = lv_obj_create(container);
    lv_obj_set_size(btn_container, lv_pct(90), 60);
    lv_obj_set_flex_flow(btn_container, LV_FLEX_FLOW_ROW);
    lv_obj_set_flex_align(btn_container, LV_FLEX_ALIGN_SPACE_EVENLY, LV_FLEX_ALIGN_CENTER, LV_FLEX_ALIGN_CENTER);
    lv_obj_clear_flag(btn_container, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_set_style_bg_opa(btn_container, LV_OPA_TRANSP, 0);
    lv_obj_set_style_border_width(btn_container, 0, 0);
    
    // 添加按钮
    lv_obj_t *add_btn = lv_btn_create(btn_container);
    lv_obj_set_size(add_btn, 80, 40);
    lv_obj_t *add_label = lv_label_create(add_btn);
    lv_label_set_text(add_label, "添加");
    lv_obj_set_style_text_font(add_label, font_chinese, 0);
    lv_obj_set_style_bg_color(add_btn, lv_color_hex(0x4CAF50), 0); // 绿色
    lv_obj_add_event_cb(add_btn, add_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 删除按钮
    lv_obj_t *del_btn = lv_btn_create(btn_container);
    lv_obj_set_size(del_btn, 80, 40);
    lv_obj_t *del_label = lv_label_create(del_btn);
    lv_label_set_text(del_label, "删除");
    lv_obj_set_style_text_font(del_label, font_chinese, 0);
    lv_obj_set_style_bg_color(del_btn, lv_color_hex(0xF44336), 0); // 红色
    lv_obj_add_event_cb(del_btn, delete_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 查找按钮
    lv_obj_t *search_btn = lv_btn_create(btn_container);
    lv_obj_set_size(search_btn, 80, 40);
    lv_obj_t *search_label = lv_label_create(search_btn);
    lv_label_set_text(search_label, "查找");
    lv_obj_set_style_text_font(search_label, font_chinese, 0);
    lv_obj_set_style_bg_color(search_btn, lv_color_hex(0x2196F3), 0); // 蓝色
    lv_obj_add_event_cb(search_btn, search_btn_event_cb, LV_EVENT_CLICKED, NULL);

    // 修改按钮
    lv_obj_t *modify_btn = lv_btn_create(btn_container);
    lv_obj_set_size(modify_btn, 80, 40);
    lv_obj_t *modify_label = lv_label_create(modify_btn);
    lv_label_set_text(modify_label, "修改");
    lv_obj_set_style_text_font(modify_label, font_chinese, 0);
    lv_obj_set_style_bg_color(modify_btn, lv_color_hex(0xFF9800), 0); // 橙色
    lv_obj_add_event_cb(modify_btn, modify_btn_event_cb, LV_EVENT_CLICKED, NULL);
    
    // 结果标签
    result_label = lv_label_create(container);
    lv_label_set_text(result_label, "");
    lv_obj_set_style_text_font(result_label, font_chinese, 0);
    lv_obj_set_style_text_color(result_label, lv_color_white(), 0);
    lv_obj_align(result_label, LV_ALIGN_BOTTOM_MID, 0, -10);
    
    // 键盘
    kb = lv_keyboard_create(parent);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN); // 让键盘隐藏
    
    // 为所有输入框添加键盘事件
    lv_obj_t *textareas[] = {TrainNumber_ta, start_ta, end_ta, departtime_ta, 
                            date_ta, ArriveTime_ta, price_ta, totalseats_ta, NULL};
    
    for(int i = 0; textareas[i] != NULL; i++) {
        lv_obj_add_event_cb(textareas[i], focus_cb, LV_EVENT_FOCUSED, kb);
        lv_obj_add_event_cb(textareas[i], defocus_cb, LV_EVENT_DEFOCUSED, kb);
    }
}

// 输入框获取焦点时的回调函数
static void focus_cb(lv_event_t * e)
{
    lv_obj_t * kb = (lv_obj_t *)lv_event_get_user_data(e);
    lv_obj_t * ta = lv_event_get_target(e);

    /* 1. 绑定键盘和输入框 */
    lv_keyboard_set_textarea(kb, ta);

    /* 2. 显示键盘 */
    lv_obj_clear_flag(kb, LV_OBJ_FLAG_HIDDEN);

    /* 3. 把键盘移到屏幕底部（如果之前没做过） */
    lv_obj_align(kb, LV_ALIGN_BOTTOM_MID, 0, 0);

    /* 4. 计算输入框的新位置：键盘正上方，留一点空隙 */
    lv_coord_t kb_top = lv_obj_get_y(kb);                /* 键盘顶部 y */
    lv_coord_t ta_h   = lv_obj_get_height(ta);             /* 输入框高度 */
    lv_coord_t gap    = 5;                                 /* 空隙，可改 */

    /* 5. 设置输入框坐标 */
    lv_obj_align(ta, LV_ALIGN_BOTTOM_MID, 0, -(lv_obj_get_height(kb) + ta_h + gap));
}

// 输入框失去焦点时的回调函数
static void defocus_cb(lv_event_t * e)
{
    lv_obj_t * kb = (lv_obj_t *)lv_event_get_user_data(e);
    lv_obj_add_flag(kb, LV_OBJ_FLAG_HIDDEN);
}